package cfca.seal.web.service;

import cfca.seal.bean.AdminUser;
import cfca.seal.bean.Seal;
import cfca.seal.bean.User;
import cfca.seal.common.Page;
import cfca.seal.dao.main.IUserDao;
import cfca.seal.exception.CodeException;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

@Service
public class UserService
{

  @Resource
  private IUserDao dao;

  public Page<User> getUsers(User user, String pageNo, AdminUser manager)
    throws Exception
  {
    Page page = this.dao.getPage(user, pageNo, manager.getUserType() > 0 ? manager.getOrganizationId() : "");
    return page;
  }

  public User getUserById(String id) throws Exception {
    return this.dao.getUserById(id);
  }

  public boolean addUser(User user) throws Exception {
    List list = this.dao.getUserByCertSn(user.getCertSn());
    if ((null != list) && (list.size() > 0)) {
      throw new CodeException("C9008", "已存在证书SN相同的用户");
    }

    return this.dao.insert(user);
  }

  public boolean deleteById(String id) throws Exception {
    return this.dao.deleteById(id);
  }

  public boolean update(User user) throws Exception {
    List list = this.dao.getUserByCertSnForUpdate(user.getCertSn(), user.getId());
    if ((null != list) && (list.size() > 0)) {
      throw new CodeException("C9008", "已存在证书SN相同的用户");
    }

    return this.dao.update(user);
  }

  public List<Seal> getBindedSeals(String userId) throws Exception {
    return this.dao.getBindedSeals(userId);
  }
}